package com.by.common.crud;

import com.by.common.bean.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
public class BaseServiceImpl<T,M extends BaseMapper> implements BaseService<T> {
    @Autowired
    protected M baseMapper;

    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }


    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) baseMapper.get(id);
    }


    @Override
    public PageBean queryByMap(Map map) {
        int page=1;//当前第几页
        int pageSize=10;//默认每页记录数
        if(map.containsKey("page")){
            page=Integer.parseInt(map.get("page").toString());
        }
        if(map.containsKey("pageSize")){
            pageSize=Integer.parseInt(map.get("pageSize").toString());
        }
        PageHelper.startPage(page,pageSize);
        List list=baseMapper.queryByMap(map);
        PageInfo info=new PageInfo(list);
        return new PageBean(page,pageSize,info.getTotal(),list);
    }

}

